﻿using AutoMapper;
using MediatR;
using ZGLXM.ErrorCode;
using ZGLXM.Infra.Interface;
using ZGLXM.Infra.Repository;
using ZGLXM.Read.Api.Applicantion.Command.RBAC;
using ZGLXM.Read.Api.Dtos;

namespace ZGLXM.Read.Api.Applicantion.Handler.RBAC
{
    public class GetRoleCommandHandler : IRequestHandler<GetRoleCommand, ApiResult>
    {
        private readonly IRoleRepository roleRepository;
        private readonly IMapper mapper;
        public GetRoleCommandHandler(IRoleRepository roleRepository, IMapper mapper)
        {
            this.roleRepository = roleRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> Handle(GetRoleCommand request, CancellationToken cancellationToken)
        {
            var role = await roleRepository.GetAllAsync(x => x.IsDeleted == false);
            var res = mapper.Map<IList<GetRoleDTO>>(role);
            return ApiResult.OK(res);
        }
    }
}
